package com.zhida.smartcontrol.dialog.kzlxdialog;

import android.text.TextUtils;

import com.zhida.smartcontrol.common.Api;
import com.zhida.smartcontrol.common.Const;
import com.zhida.smartcontrol.common.bean.LampBean;
import com.zhida.smartcontrol.knet.KalleCallBack;
import com.zhida.smartcontrol.mvp.BasePresenterImpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class KzlxDialogPresenter extends BasePresenterImpl<KzlxDialogContract.View> implements KzlxDialogContract.Presenter {

    @Override
    public void changeSet(String switchType, List<LampBean> codeList, String type) {
        // 适用于集控、二代开关、NB 单双灯
        String url = switchType.equals(Const.ZIG_BEE_SINGLE) ? Api.HAND_AUTO_SET_ZIGBEE : Api.HAND_AUTO_SET;

        List<String> list = new ArrayList<>();
        for (LampBean lampBean : codeList) {
            if (lampBean.isCheck()) {
                if (switchType.equals(Const.ZIG_BEE_SINGLE)) {
                    list.add(lampBean.getId());
                } else {
                    list.add(lampBean.getCode());
                }
            }
        }
        if (list.size() <= 0) {
            mView.showMsg("请选择要操作的设备");
            return;
        }
        if (TextUtils.isEmpty(type)) {
            mView.showMsg("请选择一个控制类型");
            return;
        }
        Map<String, Object> params = new HashMap<>();
        params.put("codeList", list);
        params.put("type", type);//1手动，0自动
        mKalleCenter.postJson(url, params, false,5000, new KalleCallBack() {
            @Override
            public void onSuccess(String result, String msg) {
                mView.showMsg(msg);
                mView.onSuccess();
                mView.hideLoading();
            }

            @Override
            public void onFailed(String msg) {
                mView.showMsg(msg);
            }
        });
    }
}
